package edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.Function;

import edu.stanford.bmir.protege.web.client.project.Project;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.cutter.CancelCutterResult;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.cutter.CuttingResult;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.cutter.RemoveEntityResult;
import edu.stanford.bmir.protege.web.client.ui.library.msgbox.MessageBox;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.common.GWTExtUtil;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.common.StkosPanelType;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.common.interfaces.StkosCallBack;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.accordingdesc.StkosCutterAccordingDescriptionPortlet;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.accordingtree.StkosCutterAccordingTreePortlet;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.currentdesc.StkosCutterCurrentDescriptionPortlet;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.currenttree.StkosCutterCurrentTreePortlet;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.option.StkosCutterOptionPortlet;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.option.StkosCutterOptionView;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.option.StkosCutterOptionView.StkosCutterOptionViewButtonType;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.option.StkosCutterOptionView.StkosCutterOptionViewListBoxType;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.option.StkosCutterOptionView.StkosCutterOptionViewRadioButtonType;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.statistical.StkosCutterStatisticalPortlet;
import edu.stanford.bmir.protege.web.client.ui.tab.AbstractTab;
import edu.stanford.bmir.protege.web.shared.project.ProjectDetails;

/**
 * Stkos Cutter Tab
 * 
 * @author LWS
 * 
 */
public class StkosCutterTab extends AbstractTab {

	/** Column 1 **/
	// Row 1--- 选项
	private StkosCutterOptionPortlet optionPortlet;

	// Row 2--- 当前本体树
	private StkosCutterCurrentTreePortlet currentTreePortlet;

	// Row 3--- 当前本体描述
	private StkosCutterCurrentDescriptionPortlet currentDescriptionPortlet;

	/** Column 2 **/
	// Row 1--- 统计结果
	private StkosCutterStatisticalPortlet statisticalPortlet;

	// Row 2--- 参照本体树
	private StkosCutterAccordingTreePortlet accordingTreePortlet;

	// Row 3--- 参照本体描述
	private StkosCutterAccordingDescriptionPortlet accordingDescriptionPortlet;

	public StkosCutterTab(Project project) {
		super(project);
	}

	@Override
	public void setup() {
		super.setup();
		setLabel("裁切工具");

		optionPortlet = (StkosCutterOptionPortlet) getPortletByClassName(StkosCutterOptionPortlet.class
				.getName());
		currentTreePortlet = (StkosCutterCurrentTreePortlet) getPortletByClassName(StkosCutterCurrentTreePortlet.class
				.getName());
		currentDescriptionPortlet = (StkosCutterCurrentDescriptionPortlet) getPortletByClassName(StkosCutterCurrentDescriptionPortlet.class
				.getName());

		statisticalPortlet = (StkosCutterStatisticalPortlet) getPortletByClassName(StkosCutterStatisticalPortlet.class
				.getName());
		accordingTreePortlet = (StkosCutterAccordingTreePortlet) getPortletByClassName(StkosCutterAccordingTreePortlet.class
				.getName());
		accordingDescriptionPortlet = (StkosCutterAccordingDescriptionPortlet) getPortletByClassName(StkosCutterAccordingDescriptionPortlet.class
				.getName());

		if (optionPortlet == null || currentTreePortlet == null
				|| currentDescriptionPortlet == null
				|| statisticalPortlet == null || accordingTreePortlet == null
				|| accordingDescriptionPortlet == null) {
			GWT.log("Some portlet can not init.");
			return;
		}

		setControllingPortlet(optionPortlet);

		registerTreeEventHandler();
		setOptionViewEventHandler();
	}

	protected void registerTreeEventHandler() {
		currentTreePortlet.addListener(GWTExtUtil.EVENT_ONSELECTIONCHANGE,
				new Function() {
					@Override
					public void execute() {
						updateCurrentDescriptionPortletTreeNode();
					}
				});

		accordingTreePortlet.addListener(GWTExtUtil.EVENT_ONSELECTIONCHANGE,
				new Function() {
					@Override
					public void execute() {
						updateAccordingDescriptionPortletTreeNode();
					}
				});
	}

	protected void setOptionViewEventHandler() {
		final StkosCutterOptionView optionView = optionPortlet.getOptionView();
		// radioButton SearchCondition valueChange handler
		StkosCallBack radioButtonSearchConditionValueChangeHandler = new StkosCallBack() {
			@Override
			public void onCallBack(Object... objects) {
				currentTreePortlet.setShowMapper(false);

				accordingTreePortlet.removeAll();
				accordingDescriptionPortlet.removeAll();
			}
		};
		optionView.setRadioButtonHandler(
				StkosCutterOptionViewRadioButtonType.SEARCHCONDITION,
				radioButtonSearchConditionValueChangeHandler);
		// radioButton ReferToProject valueChange handler
		StkosCallBack radioButtonReferToProjectValueChangeHandler = new StkosCallBack() {
			@Override
			public void onCallBack(Object... objects) {
				currentTreePortlet.setShowMapper(true);

				accordingTreePortlet.showPanel(accordingTreePortlet
						.getActivityPanelType());
			}
		};
		optionView.setRadioButtonHandler(
				StkosCutterOptionViewRadioButtonType.REFERTOPROJECT,
				radioButtonReferToProjectValueChangeHandler);

		// listBox ReferToOntology change handler
		StkosCallBack listBoxReferToOntologyChangeHandler = new StkosCallBack() {
			@Override
			public void onCallBack(Object... objects) {
				ProjectDetails pd = optionView.getReferToProjectDetails();
				if (pd != null && pd.getProjectId() != null) {
					AsyncCallback<Project> callback = new AsyncCallback<Project>() {
						@Override
						public void onFailure(Throwable caught) {
							updateAccordingProject(null);
						}

						@Override
						public void onSuccess(Project result) {
							updateAccordingProject(result);
						}
					};
					GWTExtUtil.getProject(pd, callback);
				} else {
					updateAccordingProject(null);
				}
			}
		};
		optionView.setListBoxHandler(
				StkosCutterOptionViewListBoxType.ACCORDINGONTOLOGY,
				listBoxReferToOntologyChangeHandler);

		// button AutoCutter click handler
		StkosCallBack buttonAutoCutterClickHandler = new StkosCallBack() {
			@Override
			public void onCallBack(Object... objects) {
				CuttingResult result = null;
				if (objects != null && objects.length != 0
						&& objects[0] != null) {
					result = (CuttingResult) objects[0];
				} else {
					MessageBox.showAlert("提示", "自动裁切返回结果为空!");
				}
				statisticalPortlet.getStatisticalView()
						.setCuttingResult(result);

				currentTreePortlet.onRefresh();
				currentDescriptionPortlet.onRefresh();

				accordingTreePortlet.onRefresh();
				accordingDescriptionPortlet.onRefresh();
			}
		};
		optionView.setBtnHandler(StkosCutterOptionViewButtonType.AUTOCUTTER,
				buttonAutoCutterClickHandler);
		// button ShowClass click handler
		StkosCallBack buttonShowClassClickHandler = new StkosCallBack() {
			@Override
			public void onCallBack(Object... objects) {
				currentTreePortlet.showPanel(StkosPanelType.CLASS_TREE);
				updateCurrentDescriptionPortletTreeNode();

				accordingTreePortlet.showPanel(StkosPanelType.CLASS_TREE);
				updateAccordingDescriptionPortletTreeNode();
			}
		};
		optionView.setBtnHandler(StkosCutterOptionViewButtonType.SHOWCLASS,
				buttonShowClassClickHandler);
		// button ShowProperty click handler
		StkosCallBack buttonShowPropertyClickHandler = new StkosCallBack() {
			@Override
			public void onCallBack(Object... objects) {
				currentTreePortlet.showPanel(StkosPanelType.PROPERTY_TREE);
				updateCurrentDescriptionPortletTreeNode();

				accordingTreePortlet.showPanel(StkosPanelType.PROPERTY_TREE);
				updateAccordingDescriptionPortletTreeNode();
			}
		};
		optionView.setBtnHandler(StkosCutterOptionViewButtonType.SHOWPROPERTY,
				buttonShowPropertyClickHandler);
		// button ShowIndividual click handler
		StkosCallBack buttonShowIndividualClickHandler = new StkosCallBack() {
			@Override
			public void onCallBack(Object... objects) {
				currentTreePortlet.showPanel(StkosPanelType.INDIVIDUAL_TREE);
				updateCurrentDescriptionPortletTreeNode();

				accordingTreePortlet.showPanel(StkosPanelType.INDIVIDUAL_TREE);
				updateAccordingDescriptionPortletTreeNode();
			}
		};
		optionView.setBtnHandler(
				StkosCutterOptionViewButtonType.SHOWINDIVIDUAL,
				buttonShowIndividualClickHandler);
		// button physicalCutting click handler
		StkosCallBack buttonPhysicalCuttingClickHandler = new StkosCallBack() {
			@Override
			public void onCallBack(Object... objects) {
				// MessageBox.showAlert("buttonPhysicalCuttingClickHandler");
				currentTreePortlet.onRefresh();
				currentDescriptionPortlet.onRefresh();

				CuttingResult result = null;
				if (objects != null && objects.length != 0
						&& objects[0] != null) {
					try {
						RemoveEntityResult removeEntityResult = (RemoveEntityResult) objects[0];
						result = removeEntityResult.getCuttingResult();
					} catch (Exception e) {
					}
				} else {
					MessageBox.showAlert("提示", "自动裁切返回结果为空!");
				}
				statisticalPortlet.getStatisticalView()
						.setCuttingResult(result);
			}
		};
		optionView.setBtnHandler(
				StkosCutterOptionViewButtonType.PHYSICALCUTTING,
				buttonPhysicalCuttingClickHandler);
		// button ExportResult click handler
		StkosCallBack buttonExportResultClickHandler = new StkosCallBack() {
			@Override
			public void onCallBack(Object... objects) {
				// MessageBox.showAlert("buttonExportResultClickHandler");
			}
		};
		optionView.setBtnHandler(StkosCutterOptionViewButtonType.EXPORTRESULT,
				buttonExportResultClickHandler);
		// button cancle click handler
		StkosCallBack cancleCB = new StkosCallBack() {
			@Override
			public void onCallBack(Object... objects) {
				// MessageBox.showAlert("cancleCB");
				CancelCutterResult result = null;
				try {
					result = (CancelCutterResult) objects[0];
				} catch (Exception e) {
				}

				currentTreePortlet.onRefresh();
				currentDescriptionPortlet.onRefresh();
				accordingTreePortlet.onRefresh();
				accordingDescriptionPortlet.onRefresh();
				statisticalPortlet.getStatisticalView().setCuttingResult(null);
			}
		};
		optionPortlet.getOptionView().setBtnHandler(
				StkosCutterOptionViewButtonType.CANCEL, cancleCB);
	}

	protected void updateAccordingProject(Project project) {
		accordingTreePortlet.setAccrodingProject(project);
		accordingDescriptionPortlet.setAccrodingProject(project);

		currentTreePortlet.setAccordingProject(project);
	}

	protected void updateCurrentDescriptionPortletTreeNode() {
		currentDescriptionPortlet.setSelectedTreeNode(
				currentTreePortlet.getActivityPanelType(),
				currentTreePortlet.getSelectedTreeNode());
	}

	protected void updateAccordingDescriptionPortletTreeNode() {
		accordingDescriptionPortlet.setSelectedTreeNode(
				accordingTreePortlet.getActivityPanelType(),
				accordingTreePortlet.getSelectedTreeNode());
	}
}
